﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmDepartmentList2 : FormBase
    {
        DepartmentClient wcfDepartment;
        public frmDepartmentList2()
        {
            InitializeComponent();
            //
            wcfDepartment = HRM.Service.Department();

            Init_Control();
        }

        public override void Show_Form()
        {
            this.Show();
        }


        protected override void Init_Control()
        {
            //
            dtDepartment = wcfDepartment.Department_GetWithBranch(-9999, 0).Tables[0];
            //
            Init_Grid();
            //
            Bind_Data();
        }

        //Khởi tạo danh sách cho grid
        DataGridHandle m_GridHandle;
        protected void Init_Grid()
        {
            List<DataGridViewColumn> arrColumn = new List<DataGridViewColumn>();

            //arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("STT", "RN", 30, "", true));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Mã", "Department_Code", 70));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Tên phòng ban", "Department_Name", 200));

            DataGridViewComboBoxColumn comboCol = FormGlobals.Grid_NewColumn<DataGridViewComboBoxColumn>("Trực thuộc", "Department_MasterID", 150);
            comboCol.DisplayMember = "Department_Name";
            comboCol.ValueMember = "Department_ID";
            comboCol.DataSource = dtDepartment;
            arrColumn.Add(comboCol);
            //
            m_GridHandle = FormGlobals.Grid_Init(dgvDepartment, arrColumn, "", 1);
            m_GridHandle.Row_Validated += new DataGridHandle.Row_ValidatedEventHandler(m_GridHandle_Row_Validated);
        }

        void m_GridHandle_Row_Validated(DataGridViewRow row)
        {
            (row.Cells["Department_MasterID"].OwningColumn as DataGridViewComboBoxColumn).DataSource = dtDepartment.DefaultView.ToTable();
        }

        DataTable dtDepartment;
       
        private void Bind_Data()
        {
            Program.intBranchID = Program.intBranchID == 0 ? 43 : Program.intBranchID;
            dtDepartment = wcfDepartment.Department_GetWithBranch(Program.intBranchID,0).Tables[0];
            dgvDepartment.DataSource = dtDepartment;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Bind_Data();
        }

        private void dgvDepartment_DataSourceChanged(object sender, EventArgs e)
        {
            
            //foreach (DataGridViewRow dgv in dgvDepartment.Rows)
            //{
            //    DataGridViewComboBoxColumn comboCol = dgv.Cells[
            //    comboCol.DataSource = dtDepartment;
            //}
        }

        
    }
}
